<?php defined('SYSPATH') or die('No direct script access.');

class Model_User extends ORM {
	
	protected $_table_name = 'user';
	
	protected $_primary_key = 'id';
	
	protected $_has_many = array(
		'forum_topics' => array(
			'model' => 'Forum_Topic',
			'foreign_key' => 'user_id',
		),
		'forum_messages' => array(
			'model' => 'Forum_Message',
			'foreign_key' => 'user_id',
		),
		'forum_subscribes' => array(
			'model' => 'Forum_Subscribe',
			'foreign_key' => 'user_id',
		),
	);
	
	protected $_pc_table_columns = array(
		'id' => array(
			'type' => 'INT(11)',
			'AUTO_INCREMENT' => TRUE,
		),
		'identity' => array(
			'type' => 'VARCHAR(255)',
		),
		'identity_hash' => array(
			'type' => 'VARCHAR(64)',
		),
		'email' => array(
			'type' => 'VARCHAR(64)',
		),
		'email_new' => array(
			'type' => 'VARCHAR(64)',
		),
		'photo' => array(
			'type' => 'VARCHAR(255)',
		),
		'first_name' => array(
			'type' => 'VARCHAR(64)',
		),
		'last_name' => array(
			'type' => 'VARCHAR(64)',
		),
		'nickname' => array(
			'type' => 'VARCHAR(32)',
		),
		'forum_total_messages' => array(
			'type' => 'INT(11)',
		),
		'forum_sign' => array(
			'type' => 'VARCHAR(255)',
		),
		'country' => array(
			'type' => 'VARCHAR(64)',
		),
		'city' => array(
			'type' => 'VARCHAR(64)',
		),
		'phone' => array(
			'type' => 'VARCHAR(32)',
		),
	);
	
	protected $_pc_table_indexes = array(
		'id' => array(
			'PRIMARY_KEY' => array(),
		),
		'identity' => array(
			'INDEX' => array('name' => 'identity'),
		),
		'identity_hash' => array(
			'INDEX' => array('name' => 'identity_hash'),
		),
		'email' => array(
			'INDEX' => array('name' => 'email'),
		),
		'country' => array(
			'INDEX' => array('name' => 'country'),
		),
		'city' => array(
			'INDEX' => array('name' => 'city'),
		),
	);
	
	public static function full_name(Model_User $user)
	{
		if ($user->first_name != '' OR $user->last_name != '')
		{
			return trim($user->first_name .' '. $user->last_name);
		}
		
		return 'Пользователь '. $user->id;
	}
	
	public static function nickname(Model_User $user)
	{
		if ($user->nickname == '')
		{
			return 'Пользователь #'.$user->id;
		}
		
		return $user->nickname;
	}
	
	public static function html_avatar(Model_User $user)
	{
		if ($user->photo != '')
			return '<img src="'.$user->photo.'" alt="" />';
		else
			return '<img src="'.URL::site('/media/img/empty_avatar.gif', TRUE).'" alt="" />';
	}
	
}